<?php

namespace App\Containers\AppSection\Ban\Actions;

use App\Containers\AppSection\Ban\Tasks\UpdateBanTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\FindProfileByIdTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Ship\Exceptions\BadRequestException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class UnbanProfileAction extends Action
{
    public function run($id, Request $request)
    {
        $profile = app(FindProfileByIdTask::class)->run($id);

        if ($profile->isNotBanned()) {
            throw new BadRequestException('Профиль не был заблокирован!');
        }

        app(UpdateProfileTask::class)->run($profile->id, [
            'ban_id' => null,
            'accreditation_status_key' => 'accreditation_not_accredited',
            'accredited_at' => null
        ]);

        app(UpdateBanTask::class)->run($profile->ban->id, [
            'unbanned_at' => now(),
            'unban_comment' => $request->getInputByKey('comment')
        ]);

        app(CreateLogTask::class)->run($profile->id, "Профиль №{$profile->id} разблокирован (админ)", 'ban_profile');
        app(CreateLogTask::class)->run($request->profile()->id, "Профиль №{$profile->id} разблокирован", 'ban_profile');
    }
}
